package com.example.zk

import android.annotation.SuppressLint
import android.app.ActionBar.LayoutParams
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnScrollChangeListener
import android.widget.Button
import android.widget.MediaController
import android.widget.PopupWindow
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.base.mvvm.BaseActivity
import com.example.zk.adapter.OneAdapter
import com.example.zk.adapter.TwoAdapter
import com.example.zk.databinding.ActivityMainBinding
import com.example.zk.viewModel.MainViewModel

class MainActivity : BaseActivity<ActivityMainBinding,MainViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    var isClick = false//为了判断是否是点击事件引起的滑动
    @SuppressLint("MissingInflatedId")
    override fun initView() {
        //视频播放
        binding.vv.setVideoPath("http://txmov2.a.yximgs.com/upic/2020/09/02/22/BMjAyMDA5MDIyMjAwNDZfNjU0Njc0XzM1NDM5MTg2Mjg3XzFfMw==_b_B3e4fad299fd832fc75ba67df44acb80b.mp4?tag=1-1599265874-vdg-1-r8rhfdew2y-62c7c39afc8eee5a&tt=b&bp=10000")
        val controller = MediaController(this)
        controller.setMediaPlayer(binding.vv)
        binding.vv.setMediaController(controller)
        binding.vv.start()
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(this)
        //点击三级分类，弹出弹出框
        binding.goCate.setOnClickListener {
            val popupWindow = PopupWindow()
            val view = LayoutInflater.from(this).inflate(R.layout.pop, null)
            view.findViewById<Button>(R.id.btn_close).setOnClickListener {
                popupWindow.dismiss()
            }
            val recyclerView = view.findViewById<RecyclerView>(R.id.rv)
            recyclerView.adapter = oneAdapter
            recyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
            viewModel.category()
            view.findViewById<Button>(R.id.btn_send).setOnClickListener {

            }

            popupWindow.height = 1000
            popupWindow.width = LayoutParams.MATCH_PARENT
            popupWindow.contentView =view
            popupWindow.isOutsideTouchable = true
            popupWindow.isFocusable = true

            popupWindow.showAtLocation(binding.goCate,Gravity.BOTTOM,0,0)
        }




        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        val layoutManager = LinearLayoutManager(this)//为了方便滑动监听
        binding.rvTwo.layoutManager = layoutManager

        //请求分类数据
        viewModel.category()

        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setId(oneAdapter.data[position].id)//一级文字颜色
            isClick = true//右侧滑动时，不影响一级分类
            layoutManager.scrollToPositionWithOffset(position,0)//rvTwo滑动
            isClick = false//右侧滑动时，影响一级分类
        }

        binding.rvTwo.addOnScrollListener(object:RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                //当前第一个可见的标题名称是否可一级分类选中的相同   不同的话，一级分类选中其他的
                val itemPosition = layoutManager.findFirstVisibleItemPosition()
                if(twoAdapter.data[itemPosition].id != oneAdapter.selectId){
                    oneAdapter.setId(twoAdapter.data[itemPosition].id)
                    binding.rvOne.scrollToPosition(itemPosition)
                }
            }
        })
        binding.goCar.setOnClickListener {
            startActivity(Intent(this,CarActivity::class.java))
        }

    }

    override fun initData() {
        viewModel.categorySuccess.observe(this){
            if(it.code==200){
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()

                twoAdapter.data.clear()
                twoAdapter.data.addAll(it.data)
                twoAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<MainViewModel> = MainViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_main

}